package com.kuiandjie.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kuiandjie.entity.Employee;


public class WebFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain fc) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		
		String url = request.getRequestURI();
		HttpSession session = request.getSession();
		
		//'.css' '.js' '.png' 'index.jsp' 'index.jspx'直接通过
		if(url.indexOf(".css") != -1 || url.indexOf("jquery.js") != -1 || url.indexOf(".min.js") != -1 || url.indexOf(".png") != -1 || url.indexOf("index.jsp") != -1 || url.indexOf("index.jspx") != -1 || url.indexOf(".gif") != -1) {
			fc.doFilter(request, response);
		} else {
			Employee em = (Employee) session.getAttribute("employee");
			if(em != null) {
				fc.doFilter(request, response);
			} else {
				response.sendRedirect("index.jspx");
			}
		} 
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
